package gengine.clientside.console;

import gengine.clientside.ClientApplication;
import gengine.common.console.ConsolePanel;
import gengine.common.misc.BaseApplication;
import gengine.common.misc.ClientInfo;
import gengine.common.packets.BasePacket;
import gengine.common.packets.MessagePacket;
import gengine.common.packets.PacketProcessor;
import gengine.common.packets.client.ClientInfoPacket;
import gengine.common.packets.server.InitPacket;

import java.util.Date;

/**
 * Date: 05.03.13
 * Time: 13:00
 *
 * @author Zont
 */
public class ClientConsolePacketProcessor implements PacketProcessor{
    ClientApplication app = BaseApplication.getApplication();
    ConsolePanel panel;

    public ClientConsolePacketProcessor() {
    }

    public ConsolePanel getPanel() {
        return panel;
    }

    public void setPanel(ConsolePanel panel) {
        this.panel = panel;
    }

    @Override
    public boolean Process(BasePacket packet) {
        switch (packet.getPacketType()) {
            case Message: {
                MessagePacket message = (MessagePacket) packet;
                //public
                if (message.getTargetId()==0) {
                    panel.addMessage(String.format("%s: %s",app.getClientInfoById(message.getClientId()).getName(),message.getMessage()));
                } else {
                    panel.addMessage(String.format("(whisper) %s",app.getClientInfoById(message.getTargetId()).getName(),message.getMessage()));
                }

                return true;
            }
            case ServerInitPacket: {
                InitPacket message = (InitPacket) packet;
                panel.addMessage(String.format("Server init answer: Your Id '%d' Message: '%s'\n", message.getClientId(), message.getText()));
                return true;
            }
            case ClientInfoPacket: {
                ClientInfo ci = ((ClientInfoPacket)packet).getClientInfo();
                panel.addMessage(String.format("New ClientInfo From %d Name: %s Ip: %s",ci.getId(),ci.getName(),ci.getIpAddress()));
            }
        }
        return false;
    }
}
